﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThesisSystem.Models;
using ThesisSystem.DAL;
using PagedList;
using ThesisSystem.ViewModels;

namespace ThesisSystem.Controllers
{ 
    public class GroupController : Controller
    {
        private ThesisSystemEntities db = new ThesisSystemEntities();

        //
        // GET: /Group/

        public ViewResult Index()
        {
            return View(db.JudegeInfoes.ToList());
        }

        //
        // GET: /Group/Details/5

        public ViewResult Details()
        {
           // JudgeInfo judgeinfo = db.JudegeInfoes.Find(id);
            return View();
        }

        //
        // GET: /Group/Create

        public ActionResult Create()
        {
            int teacherCount = db.Teachers.Count();
            int studentCout = db.Students.Count();
            ViewBag.teacherCount = teacherCount;
            ViewBag.studentCount = studentCout;

            return View();
        } 

        //
        // POST: /Group/Create

        [HttpPost]
        public ActionResult Create(int groupCount)
        {

         var delestudentgroup=   db.StudentGroups.ToList();
            //判断是否已有分组数据
         if (delestudentgroup.Count != 0)
         {
             foreach (var item in delestudentgroup)
             {
                 db.StudentGroups.Remove(item);
             }
             db.SaveChanges();
         }
         var deleteachergroup = db.TeacherGroups.ToList();
         if (deleteachergroup.Count !=0)
         {
             foreach (var item in deleteachergroup)
             {
                 db.TeacherGroups.Remove(item);
             }
             db.SaveChanges();
         }
          var delejudgeinfo=db.JudegeInfoes.ToList();
          if (delejudgeinfo.Count != 0)
          {
              foreach (var item in delejudgeinfo)
              {
                  db.JudegeInfoes.Remove(item);
              }
          }
        
            var group = new TeacherGroup();
           var groupstudent = new StudentGroup();
            var judgeinfo=new JudgeInfo();
            //进一法
            int teacherCount = db.Teachers.Count();
            int studentCout = db.Students.Count();
            var teachers = db.Teachers.ToList();
            var students=db.Students.ToList();
            int perTeacherGroupCount = (teacherCount%groupCount==0) ? teacherCount / groupCount: teacherCount / groupCount+1;
            int perStudentGroupCount = (studentCout % groupCount == 0) ? studentCout / groupCount : studentCout / groupCount + 1;
            List<string> teachersID = db.Teachers.Select(t => t.TeacherID).ToList();
            List<string> studentID = db.Students.Select(t => t.StudentID).ToList();
            List<Teacher> TempData=new List<Teacher>();
            List<Student> StudentTemp = new List<Student>();
            Random rand=new Random();
            int c;
            while (teachersID.Count > 0)
            {
                TempData = new List<Teacher>();
                for (int y = 1; y <= perTeacherGroupCount; y++)
                {
                    c = rand.Next(0, teachersID.Count());
                    if (c < teachersID.Count)
                    {
                        TempData.Add(teachers.Where(t => t.TeacherID == teachersID[c]).SingleOrDefault());
                        teachersID.Remove(teachersID[c]);
                    }
                }
                //处理tempData分组
                group = new TeacherGroup();
                group.Teachers = TempData;
                db.TeacherGroups.Add(group);
                db.SaveChanges();

            }
            while (studentID.Count > 0)
            {
                StudentTemp = new List<Student>();
                for (int i = 1; i <= perStudentGroupCount; i++)
                {
                    c = rand.Next(0, studentID.Count());
                    if (c < studentID.Count)
                    {
                        StudentTemp.Add(students.Where(t => t.StudentID == studentID[c]).SingleOrDefault());
                        studentID.Remove(studentID[c]);
                    }
                }
                groupstudent = new StudentGroup();
                groupstudent.Students = StudentTemp;
                db.StudentGroups.Add(groupstudent);
                db.SaveChanges();
            }

            List <int> studentgroupID=db.StudentGroups.Select(t=>t.StudentGroupID).ToList();
           
            foreach (var teachergroup in db.TeacherGroups.ToList())
            {
                judgeinfo = new JudgeInfo();
                judgeinfo.TeacherGroupID = teachergroup.TeacherGroupID;
                 c=rand.Next(0,studentgroupID.Count);
                judgeinfo.StudentGroupID=studentgroupID[c];
                studentgroupID.Remove(studentgroupID[c]);
              db.JudegeInfoes.Add(judgeinfo);

        
                    db.SaveChanges();
            
            }

            return RedirectToAction("judegDetail");
           
        }

        public ActionResult judegDetail()
        {
            var judinfoes=db.JudegeInfoes.ToList();
            return View(judinfoes);
        }
        public ActionResult see(int id)
        {
            var jud = db.JudegeInfoes.Find(id);
            ViewBag.students = db.Students.Where(t => t.StudentGroupID ==jud.StudentGroupID).ToList();
            ViewBag.teachers = db.Teachers.Where(t => t.TeacherGroupID == jud.TeacherGroupID).ToList();
            return View();
        }

        //private List<string> ToGroup(bool isTeacher) { }

        //
        // GET: /Group/Edit/5
 
        //
        public ActionResult Search(int? page, TargetsSearch targetsSearch)
        {
            var thesisType = db.ThesisTypes.ToList();
            ViewBag.TypeID = new SelectList(thesisType, "TypeID", "TypeName");

            var marking = db.Markings
                .Include(m => m.Targets).ToList();

            if (!string.IsNullOrEmpty(targetsSearch.Title))
            {
                marking = marking.Where(m => m.Title.ToUpper().Contains(targetsSearch.Title.ToUpper())).ToList();
            }
            if (!string.IsNullOrEmpty(targetsSearch.MarkType))
            {
                marking = marking.Where(m => m.MarkType.ToUpper().Contains(targetsSearch.MarkType.ToUpper())).ToList();
            }
            ViewBag.Current = targetsSearch;
            int pageSize = 3;//定义每页显示的记录数
            int pageIndex = (page ?? 1) - 1;
            //return View( targets.topToPagedList(pageIndex, pageSize));
            return View(marking.ToPagedList(pageIndex, pageSize));
        }
        public ActionResult Edit(int id)
        {
            JudgeInfo judgeinfo = db.JudegeInfoes.Find(id);
            return View(judgeinfo);
        }

        //
        // POST: /Group/Edit/5

        [HttpPost]
        public ActionResult Edit(JudgeInfo judgeinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(judgeinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(judgeinfo);
        }

        //
        // GET: /Group/Delete/5
 
        public ActionResult Delete()
        {
            //JudgeInfo judgeinfo = db.JudegeInfoes.Find(id);
            return View();
        }

        //
        // POST: /Group/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            JudgeInfo judgeinfo = db.JudegeInfoes.Find(id);
            db.JudegeInfoes.Remove(judgeinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}